package com.mynews.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.Toolbar;
import android.transition.Transition;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mynews.R;
import com.mynews.base.BaseActivity;
import com.mynews.bean.ZhihuDetailBean;
import com.mynews.mvp.presenter.DailyDetailPresenter;
import com.mynews.mvp.view.DailyDetailViewInterface;
import com.mynews.utils.HtmlUtil;

import butterknife.Bind;

import static com.mynews.utils.Constant.NEWS_ID;
import static com.mynews.utils.Constant.NEWS_IMG_URL;

/**
 * Created by Administrator on 2017/6/27.
 */

public class LatestNewsDetailActivity extends BaseActivity implements DailyDetailViewInterface {
    @Bind(R.id.detail_bar_image)
    ImageView detailBarImage;
    @Bind(R.id.detail_bar_copyright)
    TextView detailBarCopyright;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.wv_detail_content)
    WebView wvDetailContent;
    @Bind(R.id.fab)
    FloatingActionButton fab;

    private DailyDetailPresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_latestnews_details;
    }
    @Override
    protected void initData() {
        Intent intent = getIntent();
        String newsId = intent.getStringExtra(NEWS_ID);
        final String newsImgUrl = intent.getStringExtra(NEWS_IMG_URL);

        getWindow().getSharedElementEnterTransition().addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
            }
            @Override
            public void onTransitionEnd(Transition transition) {
                Glide.with(LatestNewsDetailActivity.this).load(newsImgUrl).centerCrop().into(detailBarImage);
            }
            @Override
            public void onTransitionCancel(Transition transition) {
            }
            @Override
            public void onTransitionPause(Transition transition) {
            }
            @Override
            public void onTransitionResume(Transition transition) {
            }
        });

        WebSettings settings = wvDetailContent.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);
        wvDetailContent.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //加载网络数据
        presenter = new DailyDetailPresenter(this);
        presenter.doLoadData(newsId);
    }

    public static void start(Context mContext,View view,String id,String imgUrl){
        Intent starter = new Intent(mContext, LatestNewsDetailActivity.class);
        starter.putExtra(NEWS_ID,id);
        starter.putExtra(NEWS_IMG_URL,imgUrl);
        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) mContext, view, "share_view");
        ActivityCompat.startActivity((Activity)mContext,starter,optionsCompat.toBundle());
    }

    /******************************
     * 网络数据的回调 start
     * *****************************/
    @Override
    public void dailyDetailShowContent(ZhihuDetailBean detailBean) {
        String htmlData = HtmlUtil.createHtmlData(detailBean.getBody(), detailBean.getCss(), detailBean.getJs());
        wvDetailContent.loadData(htmlData,HtmlUtil.MIME_TYPE,HtmlUtil.ENCODING);
        toolbar.setTitle(detailBean.getTitle());
        setSupportActionBar(toolbar);
    }

    @Override
    public void dailyErorr() {
    }

    /******************************
     * 网络数据的回调 end
     * *****************************/
}
